package com.hust360.percent.view;

import com.hust360.percent.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 点击 "排序" 等按钮之后出现的菜单
 * 
 * @author yangyang
 */
public class PopupMenuPane extends LinearLayout {

	/**
	 * 放置三角形的View
	 */
	UpTriangleView upTriangleView;

	public UpTriangleView getUpTriangleView() {
		return upTriangleView;
	}

	/**
	 * 整个菜单框真真实的宽高
	 */
	int w, h;

	/**
	 * 三角形尖角的水平坐标
	 */
	private int triangleLeft = 0;

	/**
	 * 默认的构造器
	 * 
	 * @param context
	 * @param attrs
	 */
	public PopupMenuPane(Context context) {
		super(context);
		upTriangleView = new UpTriangleView(context);
		this.addView(upTriangleView);
		setOrientation(LinearLayout.VERTICAL);
	}

	@SuppressWarnings("deprecation")
	public PopupMenuPane(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.upTriangleView = new UpTriangleView(context);
		this.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.popup_menu_bg));
		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		this.addView(upTriangleView, 0, layoutParams);
		setOrientation(LinearLayout.VERTICAL);
	}

	@SuppressWarnings("deprecation")
	public PopupMenuPane(Context context, View menuGroup) {
		super(context);
		// TODO Auto-generated constructor stub
		this.upTriangleView = new UpTriangleView(context);
		this.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.popup_menu_bg));
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
				upTriangleView.triangleHeight);
		this.addView(upTriangleView, 0, layoutParams);
		setOrientation(LinearLayout.VERTICAL);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		/*
		 * 计算宽度
		 */
		int mode = MeasureSpec.getMode(widthMeasureSpec);
		if (mode == MeasureSpec.EXACTLY) {
			w = MeasureSpec.getSize(widthMeasureSpec);
			Log.e(getClass().getSimpleName(), "计算宽度, EXACTLY MODE");
		} else {
			int childWidths = 0;
			View v;
			int childCount = this.getChildCount();
			for (int i = 0; i < childCount; i++) {
				v = this.getChildAt(i);
				if (this.getOrientation() == LinearLayout.HORIZONTAL) {
					childWidths += v.getWidth();
				} else {
					Log.e(getClass().getSimpleName(), v.getWidth()
							+ "--measurewidth");
					childWidths = Math.max(childWidths, v.getWidth());
				}
			}
			w = childWidths;
		}

		/*
		 * 计算高度
		 */
		mode = MeasureSpec.getMode(heightMeasureSpec);
		if (mode == MeasureSpec.EXACTLY) {
			h = MeasureSpec.getSize(heightMeasureSpec);
		} else {
			int childHeights = 0;
			View v;
			int childCount = this.getChildCount();
			for (int i = 0; i < childCount; i++) {
				v = this.getChildAt(i);
				if (this.getOrientation() == LinearLayout.VERTICAL) {
					childHeights += v.getHeight();
				} else {
					childHeights = Math.max(childHeights, v.getHeight());
				}
			}
			h = childHeights;
		}

		// 确定计算的高度
		setMeasuredDimension(w, h);
		upTriangleView.measure(w, upTriangleView.triangleHeight);
	}

	public int getTriangleLeft() {
		return triangleLeft;
	}

	public void setTriangleLeft(int triangleLeft) {
		this.triangleLeft = triangleLeft;
	}

	/**
	 * 包含小三角形的View
	 * 
	 * @author yangyang
	 * 
	 */
	public class UpTriangleView extends View {

		Bitmap upTriangle;
		int triangleWidth;
		int triangleHeight;

		public int getTriangleWidth() {
			return triangleWidth;
		}

		public int getTriangleHeight() {
			return triangleHeight;
		}

		Paint paint;

		public UpTriangleView(Context context, AttributeSet attrs) {
			super(context, attrs);
			// TODO Auto-generated constructor stub
			upTriangle = BitmapFactory.decodeResource(getResources(),
					R.drawable.arrow_up_new);
			triangleHeight = upTriangle.getHeight();
			triangleWidth = upTriangle.getWidth();
			paint = new Paint();
		}

		public UpTriangleView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			upTriangle = BitmapFactory.decodeResource(getResources(),
					R.drawable.arrow_up_new);
			triangleHeight = upTriangle.getHeight();
			triangleWidth = upTriangle.getWidth();
			paint = new Paint();
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub
			super.onDraw(canvas);
			canvas.drawColor(Color.parseColor("#00000000"));
			canvas.drawBitmap(upTriangle, triangleLeft, 0, paint);
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			// TODO Auto-generated method stub
			setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
					triangleHeight);
		}
	}

	@Override
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		// TODO Auto-generated method stub
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
		if (!gainFocus) {
			this.setVisibility(GONE);
		}
	}

	public void hide() {
		this.setFocusable(false);
		this.setFocusableInTouchMode(false);
		this.setClickable(false);
		this.setVisibility(View.GONE);
	}

}
